﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using ComputerVision.Data;
using ComputerVision.Data.Repositories;

namespace ComputerVision.Services
{
    public class TinhHinhChayNoService : IService<TinhHinhChayNo>
    {
        private readonly IEntityRepository<TinhHinhChayNo> _tinhHinhChayNoRepository;
        public TinhHinhChayNoService(IEntityRepository<TinhHinhChayNo> tinhHinhChayNoRepository)
        {
            _tinhHinhChayNoRepository = tinhHinhChayNoRepository;
        }

        public TinhHinhChayNo Create(TinhHinhChayNo t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _tinhHinhChayNoRepository.InsertOnCommit(t);
                _tinhHinhChayNoRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return t;
        }

        public TinhHinhChayNo Update(TinhHinhChayNo t)
        {
            TinhHinhChayNo item;
            try
            {
                item = _tinhHinhChayNoRepository.Get(t.Id);
                item.Ngay = t.Ngay;
                item.NoiPhatSinhChayNo = t.NoiPhatSinhChayNo;
                item.NguyenNhan = t.NguyenNhan;
                item.Chet = t.Chet;
                item.BiThuong = t.BiThuong;
                item.TaiSan = t.TaiSan;
                item.XuLyHanhChinh = t.XuLyHanhChinh;
                item.XuLyHanhChinh = t.XuLyHanhChinh;
                item.KhoiToVuAn = t.KhoiToVuAn;
                //item.Created = DateTime.Now;
                item.Modified = DateTime.Now;
                //item.CreatedBy = t.CreatedBy;
                item.ModifiedBy = t.ModifiedBy;
                item.PhieuDieuTraCoSoId = t.PhieuDieuTraCoSoId;
                _tinhHinhChayNoRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine +
                               string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                                   validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return item;
        }

        public TinhHinhChayNo Detail(Guid id)
        {
            var item = _tinhHinhChayNoRepository.Get(id);
            return item;
        }

        public List<TinhHinhChayNo> ToList()
        {
            var item = _tinhHinhChayNoRepository.GetAll().ToList();
            return item;
        }

        public void Delete(TinhHinhChayNo t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _tinhHinhChayNoRepository.DeleteOnCommit(t);
                _tinhHinhChayNoRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
        }
    }
}
